﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Xml.Schema;
using UnityEngine;
using Random = UnityEngine.Random;

public class ProcessManager : MonoBehaviour
{
    public DialogManager dialogue;
    [SerializeField] private GameObject enemy,enemyfast;
    private CameraMana cameraManager;
    private bool didSpawnEnemy = false;

    private ResourceManager resM;

    [SerializeField] private float hardestTime = 4800, levelTimer;

    [SerializeField] private float spawnTimer, spawnTime = 240,ratio;
    // Start is called before the first frame update
    void Start()
    {
        dialogue = this.gameObject.GetComponent<DialogManager>();
        cameraManager = this.gameObject.GetComponent<CameraMana>();
        resM = this.gameObject.GetComponent<ResourceManager>();
        Application.targetFrameRate = 60;
    }

    // Update is called once per frame
    void Update()
    {
        dialogue.loadText(dialogue.dialogIndex);
        if (dialogue.dialogIndex == 1)
        {
            cameraManager.isAnimation = false;
            cameraManager.isFree = true;
        }
        if (dialogue.dialogIndex == 2)
        {
            if (!didSpawnEnemy)
            {
                Instantiate(enemy, new  Vector3(9.77f, 1, -1.19f),Quaternion.identity);
                didSpawnEnemy = true;
            }
        }
        if (dialogue.dialogIndex == 3)
        {
            didSpawnEnemy = false;
            Destroy(GameObject.Find("Enemy(Clone)"));
        }
        if (dialogue.dialogIndex == 6)
        {
            
            if (!didSpawnEnemy)
            {
                Instantiate(enemy, new  Vector3(9.77f, 1, -1.19f),Quaternion.identity);
                didSpawnEnemy = true;
            }
        }

        if (dialogue.dialogIndex < 7)
        {
            resM.regenTimer = 0;
        }
        if (dialogue.dialogIndex == 7)
        {
            didSpawnEnemy = false;
        }
        if (dialogue.dialogIndex == 8)
        {
            
            if (!didSpawnEnemy)
            {
                resM.dirtSource += 175;
                didSpawnEnemy = true;
            }
        }
        if (dialogue.dialogIndex == 9)
        {
            didSpawnEnemy = false;
        }
        if (dialogue.dialogIndex == 10)
        {
            
            if (!didSpawnEnemy)
            {
                Instantiate(enemyfast, new  Vector3(15f, 1, -1.19f),Quaternion.identity);
                didSpawnEnemy = true;
            }
        }
        if (dialogue.dialogIndex == 11)
        {
            didSpawnEnemy = false;
        }
        if (dialogue.dialogIndex == 12)
        {
            if (!didSpawnEnemy)
            {
                Destroy(GameObject.Find("Canvas").transform.GetChild(0).gameObject);
                Destroy(GameObject.Find("Canvas").transform.GetChild(1).gameObject);
                Destroy(GameObject.Find("Canvas").transform.GetChild(2).gameObject);
                didSpawnEnemy = true;
            }
        }
        if (dialogue.dialogIndex >= 11)
        {

            mainGoo();
        }
    }

    void mainGoo()
    {
        levelTimer++;
        spawnTimer++;
        ratio = levelTimer / hardestTime;
        float rand = Random.Range(0f, 360f);
        if(GameObject.FindGameObjectsWithTag("Enemy").Length <= levelTimer/1200)
        {
            Instantiate(enemy, new Vector3(25f*(float)Math.Sin(rand),1,25f*(float)Math.Cos(rand)), Quaternion.identity);
        }
        /*if(GameObject.FindObjectsOfType<>("Enemy").Length <= levelTimer/3000)
        {
            Instantiate(enemyfast, new Vector3(25f*(float)Math.Sin(rand),1,25f*(float)Math.Cos(rand)), Quaternion.identity);
        }
        /*if (spawnTimer > spawnTime/(ratio*2))
        {
            Instantiate(enemy, new Vector3(25f*(float)Math.Sin(rand),1,25f*(float)Math.Cos(rand)), Quaternion.identity);
        }
        /*if (Random.Range(0f, 1f) < ratio)
        {
            Instantiate(enemy, new Vector3(25f*(float)Math.Sin(rand),1,25f*(float)Math.Cos(rand)), Quaternion.identity);
        }*/
    }
}
